package com.xinchao.job.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xinchao.common.vo.WeatherVO;
import com.xinchao.core.config.DefaultWeatherCityConfig;
import com.xinchao.job.service.NewsService;
import com.xinchao.job.service.WeatherService;

/**
 * @ClassName GetInfoController
 * @Description 提供给终端获取新闻和天气信息的接口
 * @Author HanQinrui
 * @Date 2018/9/4 14:49
 * @Version 1.0
 **/
@Controller
@RequestMapping("/getInfo")
public class GetInfoController {
    private static Logger logger = LoggerFactory.getLogger(SmsController.class);
    @Autowired
    private NewsService newsService;
    @Autowired
    private WeatherService weatherService;

    /**
     * 终端获取新闻信息
     * @param charSize 每条新闻长度
     * @param newsSize 新闻条数
     * @return
     */
    @ResponseBody
    @RequestMapping("/getNews")
    public Object getNews(HttpServletRequest request, Integer charSize, Integer newsSize) {
        logger.info("###@getNews invoke params "  + JSON.toJSONString(request.getParameterMap()));
        List<String> newsList = Lists.newArrayList();
        try {
            charSize = charSize == null ? 30 : charSize;
            newsSize = newsSize == null ? 8 : newsSize;
            newsList = newsService.getNews(charSize, newsSize);
            Collections.sort(newsList, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o1.length() >= o2.length() ? 1 : -1;
                }
            });
            return getHashResult(0, "成功", newsList);
        } catch (Exception e) {
            logger.info("getNews失败："  + e.getMessage());
            return getHashResult(1, "获取新闻信息失败：" + e.getMessage(), null);
        }
    }

    /**
     * 终端获取天气信息
     * @param request
     * @param machineNum 机器编码
     * @return
     */
    @ResponseBody
    @RequestMapping("/getWeather")
    public Object getWeather(HttpServletRequest request, String machineNum) {
        logger.info("###@getWeather invoke params "  + JSON.toJSONString(request.getParameterMap()));
        try {
            if(StringUtils.isBlank(machineNum)) {
                return getHashResult(1, "设备编码不能为空，请重试", null);
            }
            if(machineNum.length() >= 2) {
                machineNum = machineNum.substring(0,2);
            }
            // 通过机器码前两位查询到所属城市名称，用于查询天气
            String cityName = null;
            List<HashMap<String, String>> weatherCityList = DefaultWeatherCityConfig.weatherCityList;
            for(HashMap<String, String> map : weatherCityList) {
                if(machineNum.equals(map.get("cityCode"))) {
                    cityName = map.get("cityName");
                    break;
                }
            }
            if(StringUtils.isBlank(cityName)) {
                // 默认查询成都
                cityName = "成都市";
            }
            WeatherVO weatherVO = weatherService.getWeatherInfo(cityName);
            if(weatherVO == null) {
                return getHashResult(1, "当前城市未查询到天气信息，请重试", null);
            }
            return getHashResult(0, "成功", weatherVO);
        } catch (Exception e) {
            return getHashResult(1, e.toString(), null);
        }
    }

    private HashMap<String,Object> getHashResult(int code, String msg, Object data) {
        HashMap<String,Object> resultMap = new HashMap<>();
        resultMap.put("code", code);
        resultMap.put("msg", msg);
        resultMap.put("data",data);
        return resultMap;
    }
}
